﻿using GoodShop.Shops;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace GoodShop.Shop
{
    public class ShopService:CrudAppService<CommodityInfo,ShopDTO,Guid, PagedAndSortedResultRequestDto,CreateUpdateShopDTO>,IShopService 
    {
        IRepository<CommodityInfo, Guid> _shoprepository;
        IRepository<CommoditySpecifica, int> _shopbrandrepository;
        IRepository<CommodityType, int> _shoptyperepository;
        public ShopService(IRepository<CommodityInfo, Guid> repository, 
             IRepository<CommodityInfo, Guid> shoprepository,
             IRepository<CommoditySpecifica, int> shopbrandrepository,
             IRepository<CommodityType, int> shoptyperepository) : base(repository)
        {
            _shoprepository = shoprepository;
            _shopbrandrepository = shopbrandrepository;
            _shoptyperepository = shoptyperepository;
        }
        [HttpGet,Route("api/Shop/List")]
        public async  Task<ShopListDTO> ShopList()
        {

           
           var   res = from a in await  _shoprepository.GetListAsync()
                       join b in await _shopbrandrepository.GetListAsync() on a.CommodSpeciId equals b.Id
                       join c in await _shoptyperepository.GetListAsync() on b.CommodTypeId equals c.Id
                       select new ShopListDTO
                       {
                           CommodityName = a.CommodityName,
                           CommodityState=a.CommodityState,
                           CommodityDetails=a.CommodityDetails,
                           CommoditySalePrice=a.CommoditySalePrice,
                           CommodSpeciName=b.CommodSpeciName,
                           CommodityTypeName=c.CommodTypeName
                       };
            ShopListDTO obj = new ShopListDTO();
            //obj.Items = res.ToList();
            //obj.TotalCount = (await _shoprepository.GetListAsync()).Count();
            return obj;
        }
        public int add(CreateUpdateShopDTO dto)
        {
            var obj = ObjectMapper.Map<CreateUpdateShopDTO, CommodityInfo>(dto);
            for (int i = 0; i < dto.Img.Length; i++)
            {
                if (i == 0) { obj.img1 = dto.Img[0].ToString(); }
                if (i == 1) { obj.img2 = dto.Img[1].ToString(); }
                if (i == 2) { obj.img3 = dto.Img[2].ToString(); }
            }


            Repository.InsertAsync(obj);
            return 1;
        }
    }
}
